package com.qidian.permission.service.Impl;

import com.qidian.permission.entity.Permission;
import com.qidian.permission.mapper.PermissionMapper;
import com.qidian.permission.service.PermissionService;
import com.qidian.permission.util.entity.PerResult;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author 18林梦雅
 * @Company 起点编程
 * @Time 2023/10/18 20:14
 * @Description
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public PerResult addPermission(Permission permission) {
        //先判断是否存在权限,不存在则添加
        Permission permissionTemp=permissionMapper.selectByPermissionName(permission.getPermissionName());
        if(permissionTemp==null){
            return PerResult.ok("添加成功",permissionMapper.addPermission(permission));
        }
        return PerResult.duplicate("权限名已存在");
    }

//    根据父权限编号查询权限列表的接口
    @Override
    public PerResult selectAllByParentId(Long parentId) {
        List<Permission> list=permissionMapper.selectAllByParentId(parentId);
        System.out.println("==============================================");
        System.out.println(list);
        if(Objects.nonNull(list)){
            return PerResult.ok(list);
        }

        return PerResult.empty();
    }

    //根据编号查询权限的接口
    @Override
    public PerResult selectByPermissionId(Long permissionId) {
        Permission permission=permissionMapper.selectAllByPermissionId(permissionId);
        if(permission!=null){
            return PerResult.ok(permission);
        }
        return PerResult.empty("无结果");
    }

    // 根据编号编辑权限的接口
    @Override
    public PerResult updateByPermissionId(Permission permission) {
        //查询name是否存在,如果存在则比较id是否是原来的id
        System.out.println(permission+"+++++++++++++++++++++++++++++++++");

            Permission permissionTemp=permissionMapper.selectByPermissionName(permission.getPermissionName());
            if(permissionTemp!=null&&!permissionTemp.getPermissionId().equals(permission.getPermissionId())){
                return PerResult.duplicate("用户名重复");
            }
            return PerResult.updateIsOk(permissionMapper.updatePermissionById(permission));


    }




    @Override
    public PerResult queryByUserId(Long userId) {
        return PerResult.ok(permissionMapper.queryByUserId(userId));
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    @Override
    public PerResult selectAll() {
        // 先查询1级别类目  这里调用了一个递归查询的方法
        // 获取PermissionServiceImpl的代理对象
        PermissionService permissionService = (PermissionService) AopContext.currentProxy();

        List<Permission> permissions = permissionService.selectByParentId(0L);
        return PerResult.ok(permissions);
    }

    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Permission> selectByParentId(Long parentId){
        PermissionService permissionService = (PermissionService) AopContext.currentProxy();
        List<Permission> permissions = permissionMapper.selectAllByParentId(parentId);
        for (Permission permission : permissions){
            if(permission.getParent().equals(new Integer(2))){
                // 有子权限，递归查询
                permission.setChildren(permissionService.selectByParentId(permission.getPermissionId()));
            }
        }
        return permissions;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PerResult settingUserPermission(Long userId, Long[] permissionIds) {
        // 删除之前的权限
        permissionMapper.deleteRelationByUserId(userId);
        int count = permissionMapper.insertUserPermissionRelation(userId, permissionIds);
        return PerResult.ok();
    }

    @Override
    public PerResult queryByRoleId(Long roleId) {
        return PerResult.ok(permissionMapper.queryByRoleId(roleId));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public PerResult settingRolePermission(Long roleId, Long[] permissionIds) {
        // 删除之前的权限
        permissionMapper.deleteRelationByRoleId(roleId);
        int count = permissionMapper.insertRolePermissionRelation(roleId, permissionIds);
        return PerResult.ok();
    }



    @Override
    public PerResult deleteUpdateSelect(Long[] permissionIds) {
        int count=permissionMapper.deleteUpdateSelect(permissionIds);
        return PerResult.ok(count+" ");
    }






}
